package com.demo.controller;

import cn.hutool.core.util.RandomUtil;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.demo.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author shenguangyang
 */
@Slf4j
@RestController
public class TestController {
    @Resource
    private TestService testService;

    @PostConstruct
    public void init() {
        log.info("init bean {}", this.getClass().getSimpleName());
    }

    @GetMapping("/t1")
    public Resp<?> t1() throws InterruptedException {
        TimeUnit.SECONDS.sleep(10);
        return Resp.ok("调用成功");
    }

    @GetMapping("/t2")
    public Resp<?> t2() {
        return Resp.ok(testService.t2());
    }

    @GetMapping("/t3")
    public Resp<?> t3() {
        int i = RandomUtil.randomInt(0, 10);
        if (i > 6) {
            System.err.println(i);
            throw new BizException(true, -1, "数据不存在");
        }
        System.out.println(i);
        return Resp.ok();
    }

    @GetMapping("/t4")
    public Resp<?> t4() {
        return Resp.ok(UUID.randomUUID().toString());
    }
}
